package com.example.administrator.liangcang.store.fragment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.databinding.library.baseAdapters.*;
import com.example.administrator.liangcang.callback.LiangCangService;
import com.example.administrator.liangcang.R;
import com.example.administrator.liangcang.databinding.FragmentThemeBinding;
import com.example.administrator.liangcang.store.bean.themebean.ThemeEntity;
import com.example.administrator.liangcang.until.BaseApplication;
import com.example.administrator.liangcang.until.CommentRecyclerAdapter;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * A simple {@link Fragment} subclass.
 */
public class ThemeFragment extends Fragment implements Callback<ThemeEntity> {
      private FragmentThemeBinding  binding;
    private CommentRecyclerAdapter<Object> adapter;
    private LiangCangService service;

    public ThemeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return DataBindingUtil.inflate(inflater,R.layout.fragment_theme, container, false).getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
         binding =DataBindingUtil.getBinding(view);
        aboutRecycler();
    }

    private void aboutRecycler() {
        adapter = new CommentRecyclerAdapter<>(getContext(),new ArrayList<>(), R.layout.theme_item, BR.themeItem);
        binding.recycler.setAdapter(adapter);
        service = ((BaseApplication) getContext().getApplicationContext()).getService();
        service.getThemeEntity(1).enqueue(this);
    }

    @Override
    public void onResponse(Call<ThemeEntity> call, Response<ThemeEntity> response) {
        adapter.addAll(response.body().getData().getItems());
    }

    @Override
    public void onFailure(Call<ThemeEntity> call, Throwable t) {
        Toast.makeText(getContext(), "ThemeFragment"+t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
